using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace BinaryFileViewer
{
    public partial class BinaryViewerCtrl : UserControl
    {
        private Byte m_columns = 16;
        private Byte[] m_data = null;
        public event ViewProgressEventHandler ViewProgress = null;

        public Byte[] Data
        {
            get { return m_data; }
            set
            {
                m_data = value;
            }
        }

        public Byte Columns
        {
            get { return m_columns; }
            set
            {
                m_columns = value;
            }
        }

        public BinaryViewerCtrl()
        {
            InitializeComponent();
        }

        public void Render(Int32 count)
        {
            richTextBox1.Text = String.Empty;
            if((0 == m_columns ) || (null == m_data))
            {
                return;
            }
            richTextBox1.SelectionStart = 0;
            for (int nIndex = 0; nIndex < count; nIndex++)
            {
                richTextBox1.SelectedText = m_data[nIndex].ToString("X2");
                if (0 == ((nIndex + 1) % m_columns))
                {
                    if (nIndex != (count - 1))
                    {
                        richTextBox1.SelectedText = "\r\n";
                    }
                }
                else
                {
                    richTextBox1.SelectedText = " ";
                }

                if((null != ViewProgress) && (0 < m_data.Length))
                {
                    ViewProgress.Invoke(this, new ViewProgressEventArgs(100.0 * ((double)nIndex) / ((double)count)));
                }
            }
        }
    }
}
